﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WEALTH.Manager.List
{
    public partial class AddInformation : BasePage
    {
        private ExcuteDao dao = new ExcuteDao();
        public string rtnbtn = string.Empty;
        public bool isGiftFlag = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            CKFinder.FileBrowser _FileBrowser = new CKFinder.FileBrowser();

            _FileBrowser.BasePath = "../ckfinder/";

            _FileBrowser.SetupCKEditor(hfEditor);
            if (!IsPostBack)
            {
                rtnbtn = "<input type=\"button\" id=\"btnBack\" value=\"返回\" onclick=\"javascript:Index.Open('Customer/InformationList.aspx?mid=" + Request.QueryString["mid"] + "');\" />";

                SetMenu();
            }

            if (!string.IsNullOrEmpty(Request.QueryString["isGift"]))
            {
                rtnbtn = "<input type=\"button\" id=\"btnBack\" value=\"返回\" onclick=\"javascript:Index.Open('Customer/InformationListGift.aspx?mid=-1');\" />";
                rblTopText_IsUse.SelectedValue = "0";
                isGiftFlag = true;
            }
        }

        private void SetMenu()
        {
            //ddlListMenu.Items.Clear();
            //ddlListMenu.Items.Add(new ListItem("", "-1"));

            //DataTable menulistdt = dao.Get_LeftMenu();
            //for (int i = 0; i < menulistdt.Rows.Count; i++)
            //{
            //    ddlListMenu.Items.Add(new ListItem(menulistdt.Rows[i]["MenuName"].ToString(), menulistdt.Rows[i]["MenuID"].ToString()));
            //}
        }
        /// <summary>
        /// 添加用户信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                //验证页面数据
                if (!CheckData())
                {
                    return;
                }

                int menuID = -1;
                string point = null;
                string count = null;
                bool isCanBuy = false;
                bool isGift = false;

                if (isGiftFlag)
                {
                    point = txtPonit.Text;
                    count = txtCount.Text;
                    isCanBuy = rblTopText_IsUse.SelectedValue == "1" ? true : false;
                    isGift = true;
                }
                else
                {
                    menuID = Convert.ToInt32(Request.QueryString["mid"]);
                }

                if (dao.Insert_Information(
                    SpecialCharHandler.HtmlEncode(txtListTitle.Text),
                    SpecialCharHandler.HtmlEncode(txtListjianjie.Text),
                    SpecialCharHandler.HtmlEncode(txtListFrom.Text),
                    menuID,
                    SpecialCharHandler.HtmlEncode(hfEditor.Text),
                    getCurrentUser().UserID,
                    point,
                    count,
                    isCanBuy,
                    isGift) == -1)
                {
                    if (isGiftFlag)
                    {
                        ReturnList("InformationListGift.aspx", "添加信息成功！");
                    }
                    else
                    {
                        ReturnList("InformationList.aspx?mid=" + Request.QueryString["mid"], "添加信息成功！");
                    }

                }
                else
                {
                    ShowMsg("添加信息失败，请确认！", 1);
                }
            }
            catch (Exception ex)
            {
                ShowMsg(ex.Message.ToString(), 1);
            }

        }
        /// <summary>
        /// 验证页面内容
        /// </summary>
        /// <returns></returns>
        private bool CheckData()
        {
            if (string.IsNullOrEmpty(txtListTitle.Text.TrimEnd()))
            {
                ShowMsg("请输入标题！", 1);
                txtListTitle.Focus();
                return false;
            }
            //if (ddlListMenu.SelectedValue.Equals("-1"))
            //{
            //    ShowMsg("请选择所属菜单！", 1);
            //    txtListjianjie.Focus();
            //    return false;
            //}
            if (string.IsNullOrEmpty(txtListjianjie.Text.TrimEnd()))
            {
                ShowMsg("请输入简介！", 1);
                txtListjianjie.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(txtListFrom.Text.TrimEnd()))
            {
                ShowMsg("请输入信息来源！", 1);
                txtListFrom.Focus();
                return false;
            }

            return true;
        }
    }
}